# -*- coding: utf-8 -*-
# @Time    : 2024/5/9 14:30
# @Author  : Alvin
# @File    : run_spider.py

import sys
sys.path.append(r"D:\python_code\爬虫学习\scrapy_service")
from spiders.note_by_id import NoteByIdSpider
from spiders.note_by_keyword import NoteByKeywordSpider
from scrapy.crawler import CrawlerProcess
from scrapy.utils.project import get_project_settings
settings = get_project_settings()

# 创建 CrawlerProcess 实例
process = CrawlerProcess(settings)
mode_to_spider = {
    'Id': NoteByIdSpider,
    'note_by_keyword': NoteByKeywordSpider
}

def start_spider(spider_name, **kwargs):
    """
    启动一个爬虫，接受爬虫名称和参数。

    :param spider_name: 爬虫名称，对应 mode_to_spider 映射中的键。
    :param kwargs: 传递给爬虫构造函数的关键字参数。
    """
    # 从映射中获取爬虫类
    SpiderClass = mode_to_spider.get(spider_name)

    if not SpiderClass:
        raise ValueError(f"No spider found with name '{spider_name}'")

    # 使用提供的参数启动爬虫
    process.crawl(SpiderClass, **kwargs)
    process.start()
def handle_result(result, **kwargs):
    return result


if __name__ == "__main__":
    # note_id = "6631193c000000001e036b26"  # 替换成你的note_id
    # SpiderRunner.run_spider("note_by_keyword", keyword="花朵", page_num=1)
    # start_spider('Id', note_id='66432809000000001e03197e')
    import uuid
    job_id = str(uuid.uuid1())
    start_spider('note_by_keyword', keyword="高效学习", job_id="123",page_num=1)